from __future__ import print_function

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

from keras.layers import Input, Dense, Lambda
from keras.models import Model
from keras import backend as K




batch_size = 100
original_dim = 784
latent_dim =2  # 隐变量取2维只是为了方便后面画图
intermediate_dim = 128
epochs = 100
img_dim = 28


# 加载MNIST数据集
f = np.load('.\mnist.npz')
x_train, y_train = f['x_train'], f['y_train']
x_test, y_test = f['x_test'], f['y_test']
f.close()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))


x = Input(shape=(original_dim,))
h = Dense(intermediate_dim, activation='relu')(x)
# 算p(Z|X)的均值和方差
z_mean = Dense(latent_dim)(h)
z_log_var = Dense(latent_dim)(h)

# 重参数技巧
def sampling(args):
    z_mean, z_log_var = args
    epsilon = K.random_normal(shape=K.shape(z_mean))
    return z_mean + K.exp(z_log_var / 2) * epsilon

# 重参数层，相当于给输入加入噪声
z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])

# 解码层，也就是生成器部分
decoder_h = Dense(intermediate_dim, activation='relu')
decoder_mean = Dense(original_dim, activation='sigmoid')
h_decoded = decoder_h(z)
x_decoded_mean = decoder_mean(h_decoded)

# 建立模型
vae = Model(x, x_decoded_mean)

# xent_loss是重构loss，kl_loss是KL loss
xent_loss = K.sum(K.binary_crossentropy(x, x_decoded_mean), axis=-1)
kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
vae_loss = K.mean(xent_loss + kl_loss)

# add_loss是新增的方法，用于更灵活地添加各种loss
vae.add_loss(vae_loss)
vae.compile(optimizer='rmsprop')
vae.summary()

# history=vae.fit(x_train,
#         shuffle=True,
#         epochs=epochs,
#         batch_size=batch_size,
#         validation_data=(x_test, None))
#
# vae.save_weights("vae-2-100.h5")
vae.load_weights("vae-2-100.h5")
# 构建encoder，然后观察各个数字在隐空间的分布
encoder = Model(x, z_mean)
#
x_test_encoded = encoder.predict(x_test, batch_size=batch_size)
plt.figure(figsize=(6, 6))
plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test, marker='.')
plt.colorbar()
plt.show()

# 构建生成器
decoder_input = Input(shape=(latent_dim,))
_h_decoded = decoder_h(decoder_input)
_x_decoded_mean = decoder_mean(_h_decoded)
generator = Model(decoder_input, _x_decoded_mean)

# 观察隐变量的两个维度变化是如何影响输出结果的
n = 15  # figure with 15x15 digits
digit_size = 28
figure = np.zeros((digit_size * n, digit_size * n))

#用正态分布的分位数来构建隐变量对
grid_x = norm.ppf(np.linspace(0.05, 0.95, n))
grid_y = norm.ppf(np.linspace(0.05, 0.95, n))
encoded_img = encoder.predict(x_test)
decoded_img = generator.predict(encoded_img)

for i in range(n):
    for j in range(n):
        z_sample = np.array([grid_x[i], grid_y[j]])
        print(z_sample.shape)
        x_decoded = generator.predict(np.random.randn(1, latent_dim))
        # x_decoded = generator.predict(z_sample.reshape((1,latent_dim)))
        digit = x_decoded.reshape(digit_size, digit_size)
        figure[i * digit_size: (i + 1) * digit_size,
               j * digit_size: (j + 1) * digit_size] = digit

plt.figure(figsize=(10, 10))
plt.axis("off")
plt.imshow(figure, cmap='Greys_r')
plt.show()


def plot_img(x_test, decoded_img):
    # 对比重构前后的图像
    import matplotlib.pyplot as plt

    n = 10
    plt.figure(figsize=(10, 2))
    for i in range(n):
        # 展示原始图像
        ax = plt.subplot(2, n, i + 1)
        plt.imshow(x_test[i+89].reshape(28, 28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
        # 展示重构后图像
        ax = plt.subplot(2, n, i + 1 + n)
        plt.imshow(decoded_img[i+89].reshape(28, 28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
    plt.show()
    return


plot_img(x_test, decoded_img)
# fig = plt.figure()
# plt.plot(history.history['loss'],label='train loss')
# plt.plot(history.history['val_loss'], label='test loss')
# plt.title('model loss')
# plt.ylabel('loss')
# plt.xlabel('epoch')
# plt.legend(loc='upper right')
# plt.show()

def plot_ori(decoded_img):
    x = 10
    y = 2
    digit_size = 28
    figure = np.zeros((digit_size * y, digit_size * x))
    for i in range(y):
        for j in range(x):
            print("%d %d",i,j)
            img = np.reshape(decoded_img[i*10+j+100], [-1, original_dim])
            print(img.shape)
            figure[i * digit_size: (i + 1) * digit_size,
            j * digit_size: (j + 1) * digit_size] = img.reshape(28, 28)
    plt.figure(figsize=(10, 10))
    plt.imshow(figure, cmap='Greys_r')
    plt.axis("off")
    plt.show()
    return

plot_ori(decoded_img)
x = 10
y = 2
digit_size = 28
figure = np.zeros((digit_size * y, digit_size * x))
for i in range(y):
    for j in range(x):
        noise_shape = (1, latent_dim)
        z_sample = np.array(np.random.randn(*noise_shape)) # randn返回具有标准正态分布的
        x_recon = generator.predict(z_sample)
        digit = x_recon[0].reshape((img_dim, img_dim))
        figure[i * digit_size: (i + 1) * digit_size,
        j * digit_size: (j + 1) * digit_size] = digit
plt.figure(figsize=(10, 10))
plt.imshow(figure, cmap='Greys_r')
plt.axis("off")
plt.show()